package com.starsea.book.ui.newpage.utils

import android.content.Context
import com.luck.picture.lib.basic.PictureSelector
import com.luck.picture.lib.config.SelectMimeType
import com.luck.picture.lib.entity.LocalMedia
import com.luck.picture.lib.interfaces.OnResultCallbackListener
import com.luck.picture.lib.style.PictureSelectorStyle
import com.lxj.xpopup.XPopup
import com.starsea.book.ui.newpage.pop.PhotoChosePop


/**
 * @author Lee
 * @createTime 2023-09-18 09 GMT+8
 * @desc :
 */
object PicSelUtils {

  fun selPic(
    max: Int = 1,
    mContext: Context,
    selList: MutableList<LocalMedia> = arrayListOf(),
    complete: (result: ArrayList<LocalMedia?>?) -> Unit
  ) {
    XPopup.Builder(mContext)
      .asCustom(
        PhotoChosePop(
          mContext = mContext,
          object : PhotoChosePop.ClickCallBack {
            override fun onClick(type: Int) {
              if (type == 1) {

                PictureSelector.create(mContext)
                  .openGallery(SelectMimeType.ofImage())
                  .setMaxSelectNum(max)
                  .setImageEngine(GlideEngine.createGlideEngine())
                  .setSelectedData(selList)
                  .setSelectorUIStyle(getStyle(mContext))
                  .forResult(object : OnResultCallbackListener<LocalMedia> {
                    override fun onResult(result: ArrayList<LocalMedia?>?) {
                      complete(result)
                    }

                    override fun onCancel() {

                    }
                  })
                //拍照
              } else {
                PictureSelector.create(mContext)
                  .openCamera(SelectMimeType.ofImage())

                  .setSelectedData(selList)

                  .forResult(object : OnResultCallbackListener<LocalMedia?> {
                    override fun onResult(result: ArrayList<LocalMedia?>?) {
                      complete(result)
                    }

                    override fun onCancel() {

                    }
                  })
              }
            }
          })
      ).show()

  }

  fun selVideo(
    type: Int = 1,
    max: Int = 1,
    mContext: Context,
    selList: MutableList<LocalMedia> = arrayListOf(),
    complete: (result: ArrayList<LocalMedia?>?) -> Unit
  ) {
    if (type == 1) {
      PictureSelector.create(mContext)
        .openGallery(SelectMimeType.ofVideo())
        .setMaxSelectNum(max)
        .setImageEngine(GlideEngine.createGlideEngine())
        .setSelectedData(selList)
        .setSelectorUIStyle(getStyle(mContext))
        .forResult(object : OnResultCallbackListener<LocalMedia> {
          override fun onResult(result: ArrayList<LocalMedia?>?) {
            complete(result)
          }

          override fun onCancel() {

          }
        })
      //拍照
    } else {
      PictureSelector.create(mContext)
        .openCamera(SelectMimeType.ofImage())
        .setSelectedData(selList)
        .forResult(object : OnResultCallbackListener<LocalMedia?> {
          override fun onResult(result: ArrayList<LocalMedia?>?) {
            complete(result)
          }

          override fun onCancel() {

          }
        })
    }
  }

  fun selAll(
    type: Int = 1,
    max: Int = 1,
    maxVideo: Int = 1,
    mContext: Context,
    selList: MutableList<LocalMedia> = arrayListOf(),
    complete: (result: ArrayList<LocalMedia?>?) -> Unit
  ) {
    if (type == 1) {
      PictureSelector.create(mContext)
        .openGallery(SelectMimeType.ofAll())
        .setMaxSelectNum(max)
        .setMaxVideoSelectNum(maxVideo)
        .setImageEngine(GlideEngine.createGlideEngine())
        .setSelectedData(selList)
        .setSelectMaxFileSize(1024 * 99)
        .setSelectMaxDurationSecond(15)
        .setSelectorUIStyle(getStyle(mContext))
        .forResult(object : OnResultCallbackListener<LocalMedia> {
          override fun onResult(result: ArrayList<LocalMedia?>?) {
            complete(result)
          }

          override fun onCancel() {

          }
        })
      //拍照
    } else {
      PictureSelector.create(mContext)
        .openCamera(SelectMimeType.ofImage())
        .setSelectedData(selList)
        .forResult(object : OnResultCallbackListener<LocalMedia?> {
          override fun onResult(result: ArrayList<LocalMedia?>?) {
            complete(result)
          }

          override fun onCancel() {

          }
        })
    }
  }

  fun selCropPic(
    max: Int = 1,
    mContext: Context,
    selList: MutableList<LocalMedia> = arrayListOf(),
    width: Float = 0f,
    height: Float = 0f,
    complete: (result: ArrayList<LocalMedia?>?) -> Unit
  ) {
    XPopup.Builder(mContext)
      .asCustom(
        PhotoChosePop(
          mContext = mContext,
          object : PhotoChosePop.ClickCallBack {
            override fun onClick(type: Int) {
              if (type == 1) {
                PictureSelector.create(mContext)
                  .openGallery(SelectMimeType.ofImage())
                  .setMaxSelectNum(max)
                  .setImageEngine(GlideEngine.createGlideEngine())
                  .setSelectedData(selList)
                  .setSelectorUIStyle(getStyle(mContext))
                  .forResult(object : OnResultCallbackListener<LocalMedia> {
                    override fun onResult(result: ArrayList<LocalMedia?>?) {
                      complete(result)
                    }

                    override fun onCancel() {

                    }
                  })
                //拍照
              } else {
                PictureSelector.create(mContext)
                  .openCamera(SelectMimeType.ofImage())
                  .setSelectedData(selList)
                  .forResult(object : OnResultCallbackListener<LocalMedia?> {
                    override fun onResult(result: ArrayList<LocalMedia?>?) {
                      complete(result)
                    }

                    override fun onCancel() {

                    }
                  })
              }
            }
          })
      ).show()

  }


  fun getStyle(mContext: Context): PictureSelectorStyle {
    var selectorStyle = PictureSelectorStyle()
//    // 主体风格
//    val numberSelectMainStyle = SelectMainStyle()
//
//
////    numberSelectMainStyle.isCompleteSelectRelativeTop = true
////    numberSelectMainStyle.isPreviewSelectRelativeBottom = true
//
//    numberSelectMainStyle.isSelectNumberStyle = true
//    numberSelectMainStyle.isPreviewSelectNumberStyle = true
//
//    numberSelectMainStyle.selectBackground = R.drawable.shape_pic_sel_bg
//    numberSelectMainStyle.previewSelectBackground = R.drawable.shape_pic_sel_bg
//
//    numberSelectMainStyle.selectNormalTextColor = ContextCompat.getColor(mContext, R.color.black)
//    numberSelectMainStyle.selectTextColor = ContextCompat.getColor(mContext, R.color.black)
//    numberSelectMainStyle.selectTextSize = 14
//    numberSelectMainStyle.selectNormalTextSize = 14
//    numberSelectMainStyle.selectNormalBackgroundResources = R.drawable.shape_sel_pic_btn_bg_normal
//    numberSelectMainStyle.selectBackgroundResources = R.drawable.shape_sel_pic_btn_bg
//
//
//    numberSelectMainStyle.selectText = "确定"
//
//    val bottomNavBarStyle = BottomNavBarStyle()
//    bottomNavBarStyle.bottomSelectNumTextColor = ContextCompat.getColor(mContext, R.color.black)
//
//    bottomNavBarStyle.bottomSelectNumResources = R.drawable.shape_pic_sel_bg
//    bottomNavBarStyle.bottomSelectNumTextSize = 14
//
//    selectorStyle.selectMainStyle = numberSelectMainStyle
//    selectorStyle.bottomBarStyle = bottomNavBarStyle



    return selectorStyle
  }


}